package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.umeng.UmengFraudScoreData;
import com.ucredit.babel.common.model.umeng.UmengFraudScoreRecord;
import com.ucredit.babel.common.model.umeng.UmengFraudV2ScoreData;
import com.ucredit.babel.common.model.umeng.UmengFraudV2ScoreRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Classname YouMengFraudScoreParse
 * @Description 友盟申请反欺诈V2 20210413-20210514
 * @Date 2021/6/9 20:35
 * @Created by lzw
 */
public class YouMengFraudScoreV2Parse extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        UmengFraudV2ScoreRecord obj = (UmengFraudV2ScoreRecord) model;
        UmengFraudV2ScoreData umengFraudV2ScoreData = obj.getData();
        String string = umengFraudV2ScoreData.getData();

        StringBuilder sb = getBaseInfo(obj, successful);

//        sb.append(umengFraudV2ScoreData.getMessage()).append(CRT001);
//        sb.append(umengFraudV2ScoreData.getCode()).append(CRT001);
//        sb.append(umengFraudV2ScoreData.getRequestId()).append(CRT001);

        JSONObject info = JSON.parseObject(string) == null ? new JSONObject() : JSON.parseObject(string);
        JSONObject data = info.getJSONObject("data") == null ? new JSONObject() : info.getJSONObject("data");

        sb.append(info.getString("message") == null ? "" : info.getString("message")).append(CRT001);
        sb.append(info.getString("code") == null ? "" : info.getString("code")).append(CRT001);
        sb.append(info.getString("requestId") == null ? "" : info.getString("requestId")).append(CRT001);

        sb.append(data.getString("score") == null ? "" : data.getString("score")).append(CRT001);
        sb.append(data.getString("tags") == null ? "" : data.getString("tags")).append(CRT001);

        key.set(getKey());
        value.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, value);


        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_youmeng_fraudv2score_inc";
    }

    @Override
    public String getType() {
        return RecordType.UMENG_FRAUD_SCORE_V2_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "友盟申请反欺诈V2";
    }
}
